//
//  ContentViewController.m
//  A段
//
//  Created by 潘君峰 on 15/4/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ContentViewController.h"
#import "SecondViewController.h"
#import "SecondTableViewCell.h"
#import "ContentTableViewCell.h"
#import "MyConnection.h"
#import "WatchContentViewController.h"

@interface ContentViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)NSMutableArray *tableArray;
@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain)UIWebView *webView;

@end

@implementation ContentViewController
- (void)dealloc
{
    [_dic release];
    [_array release];
    [_webView release];
    [_table release];
    [_tableArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.dic = [NSDictionary dictionary];
    }
    return self;
}

- (void)createTable
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    
    //行高
    [_table setRowHeight:150];
    _table.sectionHeaderHeight = 300;
    _table.delegate = self;
    _table.dataSource = self;
    //注册重用池
    [_table registerClass:[ContentTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    [_table release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@"cover_thumb_hd"]]];
    [cell.watchPhoto setImage:[UIImage imageWithData:dataImage]];
    cell.titleLabel.text = [dic objectForKey:@"title"];
    cell.pubdateLabel.text = [dic objectForKey:@"pubdate"];
    cell.summaryLabel.text = [dic objectForKey:@"summary"];
    
    return cell;
}

- (void)startConnect
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"iphone" forKey:@"ver"];
    [dic setObject:@"9" forKey:@"app_ver"];
    

    //网络请求
    __block ContentViewController *my = self;
    if (self.num == 0) {
        SecondViewController *second = [[[SecondViewController alloc] init]autorelease];
        [self.navigationController pushViewController:second animated:YES];
    } if (self.num == 1) {
        [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/column/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
            
            //解析数据
            NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [my.tableArray addObjectsFromArray:arr];
            [_table reloadData];
        }];
    } if (self.num == 2) {
        [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/opinion/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
            
            //解析数据
            NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [my.tableArray addObjectsFromArray:arr];
            [_table reloadData];
        }];
    } if (self.num == 3) {
        [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/feature/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
            
            //解析数据
            NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [my.tableArray addObjectsFromArray:arr];
            [_table reloadData];
        }];
    } if (self.num == 4) {
        [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/events/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
            
            //解析数据
            NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [my.tableArray addObjectsFromArray:arr];
            [_table reloadData];
        }];

    }
}
//点击进入详情方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [_table deselectRowAtIndexPath:indexPath animated:YES];
    
    //数据解析成字典
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    //字典取链接
    NSString *webStr = [dic objectForKey:@"link_share"];
    WatchContentViewController *watchController = [[WatchContentViewController alloc] init];
    //属性传值
    watchController.webString = webStr;
    [self.navigationController pushViewController:watchController animated:YES];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTable];
    [self startConnect];
}




@end
